/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;


import static common.Constants.UTF8;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.Column;
import pojo.Status;
import pojo.Tag;
/**
 *
 * @author tipuder
 */
public class GeneralHandling {
   
    public static Column createCol(String name, String value)
    {
        Column result = new Column();
        long time = System.currentTimeMillis();
        try {
            
            result = new Column(toByteBuffer(name));
            if (name.equals(Status.colname_createdate) || name.equals(Tag.name_col_cdate))
            {   Calendar cal = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
                result.setValue(toByteBuffer(sdf.format(cal.getTime())));
            }
            else
                result.setValue(toByteBuffer(value));
            result.setTimestamp(time);
            
        } catch (UnsupportedEncodingException ex) {
            
            Logger.getLogger(GeneralHandling.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    public static ByteBuffer toByteBuffer(String value) 
    throws UnsupportedEncodingException
    {
        return ByteBuffer.wrap(value.getBytes(UTF8));
    }
        
    public static String toString(ByteBuffer buffer) 
    throws UnsupportedEncodingException
    {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return new String(bytes, UTF8);
    }
    
    public static int toInt(ByteBuffer buffer) 
    throws UnsupportedEncodingException
    {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        String temp = new String(bytes, UTF8);
        return Integer.parseInt(temp);
    }
    
    public static java.util.UUID getTimeUUID()
    {
        return java.util.UUID.fromString(new com.eaio.uuid.UUID().toString());
    }   

    /**
    * Returns an instance of uuid.
    *
    * @param uuid the uuid
    * @return the java.util. uuid
    */
    public static java.util.UUID toUUID( byte[] uuid )
    {
        long msb = 0;
        long lsb = 0;
        assert uuid.length == 16;
        for (int i=0; i<8; i++)
            msb = (msb << 8) | (uuid[i] & 0xff);
        for (int i=8; i<16; i++)
            lsb = (lsb << 8) | (uuid[i] & 0xff);
        long mostSigBits = msb;
        long leastSigBits = lsb;

        com.eaio.uuid.UUID u = new com.eaio.uuid.UUID(msb,lsb);
        return java.util.UUID.fromString(u.toString());
    }
    
    
    /**
        * As byte array.
        *
        * @param uuid the uuid
        *
        * @return the byte[]
        */
    public static byte[] asByteArray(java.util.UUID uuid)
    {
        long msb = uuid.getMostSignificantBits();
        long lsb = uuid.getLeastSignificantBits();
        byte[] buffer = new byte[16];

        for (int i = 0; i < 8; i++) {
                buffer[i] = (byte) (msb >>> 8 * (7 - i));
        }
        for (int i = 8; i < 16; i++) {
                buffer[i] = (byte) (lsb >>> 8 * (7 - i));
        }

        return buffer;
    }     
}
